package com.naiterui.ehp.bs.pharmacist.repository.mysql;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bs.pharmacist.entity.PharmacistRecomAuditLog;

import java.util.Collection;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @Description 审核日志
 * @Author gongbaoqiang
 * @Date 2019/12/9 1:58 PM
 */
@Repository
public interface RecomAuditLogRepository extends BaseJpaRepository<PharmacistRecomAuditLog, Long> {

    /**
     * <pre>
     *     根据审核状态查询处方信息
     * </pre>
     *
     * @param status
     * @param pageable
     *
     * @return
     */
    Page<PharmacistRecomAuditLog> findByStatus(Integer status, Pageable pageable);

    /**
     * <pre>
     *     获取药师指定状态的处方审核日志
     * </pre>
     *
     * @param pharmacistId
     * @param status
     * @param pageable
     *
     * @return
     */
    @Query(value = "select t from PharmacistRecomAuditLog t join PharmacistMedicationRecom r on t.recomId = r.id" +
            " where t.pharmacistId = ?1 and t.status = ?2 and r.expireAt > ?3")
    Page<PharmacistRecomAuditLog> findByPharmacistIdAndStatus(Long pharmacistId, Integer status, Date date, Pageable pageable);

    /**
     * <pre>
     *     获取药师指定状态的处方审核日志
     * </pre>
     *
     * @param pharmacistId
     * @param status
     * @param pageable
     *
     * @return
     */
    @Query(value = "select t from PharmacistRecomAuditLog t join PharmacistMedicationRecom r on t.recomId = r.id" +
            " where t.pharmacistId = ?1 and t.status = ?2")
    Page<PharmacistRecomAuditLog> findByPharmacistIdAndStatus(Long pharmacistId, Integer status, Pageable pageable);

    @Query(value = "select t from PharmacistRecomAuditLog t join PharmacistMedicationRecom r on t.recomId = r.id" +
        " where t.pharmacistId = ?1 and t.status in (?2)")
    Page<PharmacistRecomAuditLog> findByPharmacistIdAndStatusIn(Long pharmacistId, Collection<Integer> status, Pageable pageable);

    /**
     * <pre>
     *     获取药师指定状态的处方审核日志
     * </pre>
     *
     * @param pharmacistId
     * @param status
     * @param pageable
     *
     * @return
     */
    @Query(value = "select t from PharmacistRecomAuditLog t join PharmacistMedicationRecom r on t.recomId = r.id" +
            " where t.pharmacistId = ?1 and t.preAuditStatus = ?2 and r.expireAt > ?3")
    Page<PharmacistRecomAuditLog> findByPharmacistIdAndPreAuditStatus(Long pharmacistId, Integer preAuditStatus, Date date, Pageable pageable);


    /**
     * <pre>
     *     查询药师审核过的处方（审核通过和审核不通过的）
     * </pre>
     *
     * @param pharmacistId
     * @param status
     * @param pageable
     *
     * @return
     */
    Page<PharmacistRecomAuditLog> findByPharmacistIdAndStatusGreaterThan(Long pharmacistId, Integer status, Pageable pageable);

    /**
     * <pre>
     *     获取药师的全部有效处方审核信息
     * </pre>
     *
     * @param pharmacistId
     * @param pageable
     *
     * @return
     */

    @Query(nativeQuery = true,
            value = "select tt.* from (" +
                    " select t.* from recom_audit_log t inner join dr_medication_recom r on t.recom_id = r.id  " +
                    " where t.pharmacist_id = :pharmacistId and t.status = 0 and r.expire_at > :date" +
                    " union all " +
                    " select t2.* from recom_audit_log t2 where t2.status > 0 and t2.pharmacist_id = :pharmacistId " +
                    ") tt ",
            countQuery = "select count(1) from (" +
                    " select t.* from recom_audit_log t inner join dr_medication_recom r on t.recom_id = r.id  " +
                    " where t.pharmacist_id = :pharmacistId and t.status = 0 and r.expire_at > :date" +
                    " union all " +
                    " select t2.* from recom_audit_log t2 where t2.status > 0 and t2.pharmacist_id = :pharmacistId " +
                    ") tt")
    Page<PharmacistRecomAuditLog> findByPharmacistId(@Param("pharmacistId") Long pharmacistId, @Param("date") Date date, Pageable pageable);

    /**
     * <pre>
     *     查询处方审核信息详情
     * </pre>
     *
     * @param recomId
     *
     * @return
     */
    PharmacistRecomAuditLog findFirstByRecomIdAndPharmacistId(Long recomId, Long pharmacistId);

    PharmacistRecomAuditLog findFirstByRecomId(Long recomId);

    PharmacistRecomAuditLog findFirstByRecomIdAndAllocatePharmacistId(Long recomId, Long pharmacistId);

}
